<?php

namespace App\Http\Controllers;

use App\Entities\ClassEntity;
use App\Repositories\ClassRepository;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;

class ClassController extends Controller
{
    private $classEntity;

    public function __construct(ClassRepository $classEntity)
    {
        $this->classEntity = $classEntity;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        //
        $condition = [];
        if($request->get('class_name')){
            $condition['name'] = ['like','%'.$request->get('class_name').'%'];
        }
        if($request->get('term_id')){
            $condition['term_id'] = $request->get('term_id');
        }

        if($request->get('start_time')){
            $condition['start_time'] = ['>=', (int)$request->get('start_time')];
        }

        if($request->get('end_time')){
            $condition['end_time'] = ['<=', (int)$request->get('end_time')];
        }

        if($request->get('search_type')){
            $search_type = $request->get('search_type');
            switch ($search_type) {
                case "1":
                    $list = ClassEntity::with(['term', 'course', 'members'])
                                    ->where('end_time', '<', 13)
                                    ->where($condition)
                        ->orderBy('start_time','desc')
                        ->orderBy('created_at','asc')
                                    ->get()->groupBy('start_end');
                    break;
                case "2":
                    $list = ClassEntity::with(['term', 'course', 'members'])

                        ->where('end_time', '>', 12)
                        ->where('end_time', '<', 18)
                        ->where($condition)
                        ->orderBy('start_time','desc')
                        ->orderBy('created_at','asc')
                        ->get()->groupBy('start_end');
                    break;
                case "3":
                    $list = ClassEntity::with(['term', 'course', 'members'])

                        ->where('end_time', '>', 17)
                        ->where($condition)
                        ->orderBy('start_time','desc')
                        ->orderBy('created_at','asc')
                        ->get()->groupBy('start_end');
                    break;
                default: $list = new Collection();
            }

        } else {

            if($request->get('all')){
                $list = $this->classEntity->allWithCondition(['term', 'course', 'members'], $condition);
            }else {
                $list = $this->classEntity->paginateWithCondition(10, ['term', 'course', 'members'], $condition);
            }

        }


        return response()->json([
            'errors' => false,
            'count' => $list->count(),
            'data' => $list
        ]);
    }


    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $classEntity = $this->classEntity->create($request->all());
        return response()->json([
            'errors' => false,
            'message' => '新建成功',
            'data' => $classEntity
        ]);
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Entities\ClassEntity  $classEntity
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
        $classEntity = $this->classEntity->find($id);
        return response()->json([
            'errors' => false,
            'data' => $classEntity
        ]);
    }


    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Entities\ClassEntity  $classEntity
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        $classEntity = $this->classEntity->find($id);
        $newTerm = $this->classEntity->update($classEntity, $request->all());
        return response()->json([
            'errors'=> false,
            'data' => $newTerm
        ]);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Entities\ClassEntity  $classEntity
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        $classEntity = $this->classEntity->find($id);
        $ret = $this->classEntity->destroy($classEntity);
        return response()->json([
            'errors' =>  false,
            'data' => $ret
        ]);

    }
}
